package com.sm.honkon.ui.fragment.clients_management

import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.lifecycle.observe
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.date.dayOfMonth
import com.afollestad.date.month
import com.afollestad.date.year
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.sm.honkon.R
import com.sm.honkon.app.base.BaseFragment
import com.sm.honkon.app.ext.getTimeFormat
import com.sm.honkon.app.ext.goBackHome
import com.sm.honkon.data.bean.CustomerEntity
import com.sm.honkon.databinding.FrgHandelClientsManagerBinding
import com.sm.honkon.ui.activity.MainActivity
import com.sm.honkon.ui.adapter.PopWindowAdapter
import com.sm.honkon.ui.model.HandleClientsInfoViewModel
import kotlinx.android.synthetic.main.frg_handel_clients_manager.*
import me.hgj.jetpackmvvm.ext.nav
import java.util.*

class AddHandleClientFragment :
    BaseFragment<HandleClientsInfoViewModel, FrgHandelClientsManagerBinding>() {
    override fun layoutId(): Int = R.layout.frg_handel_clients_manager


    val pickerView: TimePickerView by lazy {
        TimePickerBuilder(requireContext(),
            OnTimeSelectListener { date, _ ->
                mViewModel.user.value?.apply {
                    birthday = date.getTimeFormat("yyyy/MM/dd")
                    mViewModel.user.postValue(this)
                }
            })
            .setLayoutRes(R.layout.layout_time_picker) {
                it.findViewById<TextView>(R.id.btnCancel).setOnClickListener {
                    pickerView.dismiss()
                }
                it.findViewById<TextView>(R.id.btnSubmit).setOnClickListener {
                    pickerView.returnData()
                    pickerView.dismiss()
                }
            }
            .setRangDate(
                Calendar.getInstance().apply {
                    year = 1910
                    month = 0
                    dayOfMonth = 1
                },
                Calendar.getInstance()
            ).setDate(Calendar.getInstance())
            .setContentTextSize(10)
            .isDialog(false)
            .setOutSideCancelable(false)
            .setBgColor(Color.parseColor("#FFFFFF")).build()
    }

    val mPopWindow: PopupWindow by lazy {
        val contentView: View = LayoutInflater.from(requireContext())
            .inflate(R.layout.popwindow, null)
        PopupWindow(
            contentView,
            tv_xuexing.width, ViewGroup.LayoutParams.WRAP_CONTENT, true
        ).apply {
            this.contentView = contentView
            val recyclerView = contentView.findViewById<RecyclerView>(R.id.host_recycle)
            recyclerView.adapter = adapter
            //解决5.0以下版本点击外部不消失问题
            isOutsideTouchable = true
            setBackgroundDrawable(BitmapDrawable())
        }
    }

    val mPopWindow2: PopupWindow by lazy {
        val contentView: View = LayoutInflater.from(requireContext())
            .inflate(R.layout.popwindow, null)
        PopupWindow(
            contentView,
            tv_shouzhenneirong.width, ViewGroup.LayoutParams.WRAP_CONTENT, true
        ).apply {
            this.contentView = contentView
            val recyclerView = contentView.findViewById<RecyclerView>(R.id.host_recycle)
            recyclerView.adapter = adapter
            //解决5.0以下版本点击外部不消失问题
            isOutsideTouchable = true
            setBackgroundDrawable(BitmapDrawable())
        }
    }

    val adapter by lazy {
        PopWindowAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                if (data == mViewModel.bloods){
                    tv_xuexing.text = data[position]
                    mViewModel.user.value?.blood = data[position]
                    mPopWindow.dismiss()
                }else{
                    tv_shouzhenneirong.text = data[position]
                    mViewModel.user.value?.last_treatment_conetnt = data[position]
                    mViewModel.user.value?.first_treatment_conetnt = data[position]
                    mPopWindow2.dismiss()
                }
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.getParcelable<CustomerEntity>("user")?.let {
            mViewModel.user.value = it
            mViewModel.isEdit.set(true)
        }
        mViewModel.curHandlerEntity = (activity as MainActivity).curHandlerEntity.value
        mDatabind.click = Click()
        mDatabind.model = mViewModel
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.isMan.observe(this) {
            mViewModel.user.value?.sex = if (it) 0 else 1
        }
    }

    inner class Click {

        fun back() {
            nav().navigateUp()
        }

        fun birthday() {
            pickerView.show()
        }

        fun showBloods(view: View) {
            adapter.setNewInstance(mViewModel.bloods)
            mPopWindow.showAsDropDown(view)
        }

        fun showShouzhen(view: View) {
            adapter.setNewInstance(mViewModel.shouzhen)
            mPopWindow2.showAsDropDown(view)
        }

        fun save() {
            mViewModel.insert {
                back()
            }
        }

        fun home() {
            nav().goBackHome()
        }

        fun mannger() {
            nav().navigateUp()
        }

        fun exit() {
            System.exit(0)
        }

        fun setting() {

            nav().navigate(R.id.settingFragment)
        }
    }
}